<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Exel extends CI_Controller {
 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
 	$this->load->library('excel');
 }
 function index(){
 		
		//activate worksheet number 1
		$query = $this->muser->getMdata('Salary');         
       
		
         
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
                 
        $objPHPExcel->setActiveSheetIndex(0);
 
        // Field names in the first row
        $fields = $query->list_fields();
        $col = 0;
		$this->excel->getActiveSheet()->setTitle('Customer Report');
        foreach ($fields as $field)
        {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
			//$objPHPExcel->getActiveSheet()->getStyle($col, 1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);			
			//$objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
			//$objPHPExcel->getActiveSheet()->getStyle($col,1)->getFont()->setBold(true);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,1)->getFont()->setBold(true);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			//$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,1)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
            $col++;
        }
		$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(20);
		for ($i = 0; $i <= 20; $i++) {
			$objPHPExcel->getActiveSheet()->getColumnDimension(chr(65+$i))->setAutoSize(true);
		}
        
		$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
        // Fetching the table data
        $row = 2;
        foreach($query->result() as $data)
        {
            $col = 0;
            foreach ($fields as $field)
            {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
                $col++;
            }
             
            $row++;
        }
 		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
		//$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->setActiveSheetIndex(0);
         
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $filename='bang luong.xlsx'; 
        // Sending headers to force the user to download the file
        header('Content-Type: application/vnd.ms-excel');
       	header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
		         
        $objWriter->save('php://output');
 }
function SalaryExcel(){
		$FromDate=$this->session->userdata('FromDate');
	 	$ToDate=$this->session->userdata('ToDate');
		$this->session->unset_userdata('FromDate');
		$this->session->unset_userdata('ToDate');	
		$query=$this->muser->getMSalary($FromDate,$ToDate);	
		$this->Excel($query);
		
	}
function CustomerExcel(){
		$FromDate=$this->session->userdata('FromDate');
		$ToDate=$this->session->userdata('ToDate');
		$Lab_ID=$this->session->userdata('Lab_ID');
		
		
		$query=$this->muser->getCustomerReport($Lab_ID,$FromDate,$ToDate);
		$this->Excel($query);
}

function Excel($query){
	
		$objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
                 
        $objPHPExcel->setActiveSheetIndex(0);
 
        // Field names in the first row
        $fields = $query->list_fields();
        $col = 0;
		$this->excel->getActiveSheet()->setTitle('Salary Report');
        foreach ($fields as $field)
        {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
			//$objPHPExcel->getActiveSheet()->getStyle($col, 1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);			
			//$objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
			//$objPHPExcel->getActiveSheet()->getStyle($col,1)->getFont()->setBold(true);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,1)->getFont()->setBold(true);
			$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,1)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			//$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,1)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
            $col++;
        }
		$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(20);
		for ($i = 0; $i <= 25; $i++) {
			$objPHPExcel->getActiveSheet()->getColumnDimension(chr(65+$i))->setAutoSize(true);
		}
        
		$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
        // Fetching the table data
        $row = 2;
        foreach($query->result() as $data)
        {
            $col = 0;
            foreach ($fields as $field)
            {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
				$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $col++;
            }
             
            $row++;
        }
 		
		//$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->setActiveSheetIndex(0);
         
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $filename='bang luong.xlsx'; 
        // Sending headers to force the user to download the file
        header('Content-Type: application/vnd.ms-excel');
       	header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
		         
        $objWriter->save('php://output');
}

}
 